package handlers

import (
	"art-info-sys/models"
	"github.com/gin-gonic/gin"
)

func Add(c *gin.Context) {
	name := c.PostForm("name")
	comment := c.PostForm("comment")
	author := c.PostForm("author")

	if name == "" || comment == "" || author == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}

	data := models.Book{
		Name:    name,
		Comment: comment,
		Author:  author,
	}

	err := models.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "错误",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "成功",
		})
		return
	}
}

func List(c *gin.Context) {
	var articles []models.Book
	models.DB.Debug().Find(&articles)
	c.JSON(200, gin.H{
		"code": 200,
		"data": articles,
		"msg":  "success",
	})
	return
}
